package com.huawo.qjs.widgets;

import android.annotation.SuppressLint;
import android.graphics.Point;
import android.util.Size;

import com.huawo.qjs.QjsDependence;
import com.huawo.qjs.QjsWidget;

import java.util.ArrayList;
import java.util.List;

/**
 * 时间控件，包含了时间/日期/星期
 * 时间包含了：
 * 时：组合图
 * 分：组合图
 * 分隔符：冒号
 */
public class QjsTime extends QjsWidget {

    private Point hourPoint;
    private Point minutePoint;
    private Size hourSize;
    private Size minuteSize;
    private Point colonPoint;
    private Size colonSize;

    private Point datePoint;
    private Size dateSize;

    private Point weekPoint;
    private int width;
    private int height;

    public QjsTime(String name) {
        super(name);
    }

    /**
     * QjsTime控件名称（英文字母开头，全局唯一即可，不要太长）
     * @param name 英文字母开头，全局唯一即可，不要太长
     * @param color 控件颜色
     */
    public QjsTime(String name, int color,int width,int height) {
        super(name);
        this.setColor(color);
        this.width = width;
        this.height = height;
    }

    @Override
    public List<String> getImageNames() {
        List<String> list = new ArrayList<>();
        list.add("time_s_30_g");
        list.add("time_s_31_g");
        list.add("time_s_32_g");
        list.add("time_s_33_g");
        list.add("time_s_34_g");
        list.add("time_s_35_g");
        list.add("time_s_36_g");
        list.add("time_s_37_g");
        list.add("time_s_38_g");
        list.add("time_s_39_g");
//        list.add("time_s_2103_g");
//        list.add("time_s_2109_g");
        list.add("time_s_25");

        list.add("time_b_30_g");
        list.add("time_b_31_g");
        list.add("time_b_32_g");
        list.add("time_b_33_g");
        list.add("time_b_34_g");
        list.add("time_b_35_g");
        list.add("time_b_36_g");
        list.add("time_b_37_g");
        list.add("time_b_38_g");
        list.add("time_b_39_g");
//        list.add("time_b_2103_g");
//        list.add("time_b_2109_g");
        list.add("time_b_25");

        list.add("week_00_g");
        list.add("week_01_g");
        list.add("week_02_g");
        list.add("week_03_g");
        list.add("week_04_g");
        list.add("week_05_g");
        list.add("week_06_g");

        list.add("date_num_30_g");
        list.add("date_num_31_g");
        list.add("date_num_32_g");
        list.add("date_num_33_g");
        list.add("date_num_34_g");
        list.add("date_num_35_g");
        list.add("date_num_36_g");
        list.add("date_num_37_g");
        list.add("date_num_38_g");
        list.add("date_num_39_g");

        return list;
    }

    public String getTimeImagePrefix() {
        String prefix = "";
        String suffix = "";
        if (getStyle() == 0) {
            prefix = "time_s_";
        } else {
            prefix = "time_b_";
        }
        suffix = "" + getColor() + "_";
        return prefix + suffix;
    }

    public String getDateImagePrefix() {
        return "date_num_" + getColor() + "_";
    }

    public String getWeekImagePrefix() {
        return "week_" + getColor() + "_";
    }

    public String getColonPath() {
        String prefix = "";
        String suffix = ".bin";
        if (getStyle() == 0) {
            prefix = "time_b_25";
        } else {
            prefix = "time_s_25";
        }
        suffix = "_" + getColor() + ".bin";
        return prefix + suffix;
    }

    @Override
    public List<QjsDependence> getDependences() {
        List<QjsDependence> dependences = new ArrayList<>();
        dependences.add(new QjsDependence("label", "label.js"));
        dependences.add(new QjsDependence("idximg", "idximg.js"));
        dependences.add(new QjsDependence("img", "img.js"));
        return dependences;
    }

    @Override
    public String start() {
        // 先初始化控件位置和大小
        fitPositionAndSize();
        StringBuilder sb = new StringBuilder();
        if (getWatchfaceStyle() == 4) {
            // 日期
            String dateJS = groupJS("_date", QjsWidget.DATE_NUM, getDateImagePrefix(), datePoint, dateSize, getStyle(), index);
            // 星期
            String weekJS = optionJS("_week", QjsWidget.WEEK_DATA, getWeekImagePrefix(), weekPoint,index);

            sb.append(dateJS);
            sb.append(weekJS);
        } else {
            // 创建时钟组合图
            String hourJS = groupJS("_hour", QjsWidget.HOUR_NUM, getTimeImagePrefix(), hourPoint, hourSize, getStyle(), index);
            // 冒号
            String colonJS = imgJS("_colon", getColonPath(), colonPoint, index);
            // 分钟
            String minuteJS = groupJS("_minute", QjsWidget.MINUTE_NUM, getTimeImagePrefix(), minutePoint, minuteSize, getStyle(), index);
            // 日期
            String dateJS = groupJS("_date", QjsWidget.DATE_NUM, getDateImagePrefix(), datePoint, dateSize, getStyle(), index);
            // 星期
            String weekJS = optionJS("_week", QjsWidget.WEEK_DATA, getWeekImagePrefix(), weekPoint,index);

            sb.append(hourJS);
            sb.append(colonJS);
            sb.append(minuteJS);
            sb.append(dateJS);
            sb.append(weekJS);
        }


        return sb.toString();
    }

    private void fitPositionAndSize() {
        int watchStyle = getWatchfaceStyle();
        int widgetStyle = getStyle();
        hourPoint = new Point();
        minutePoint = new Point();
        colonPoint = new Point();
        datePoint = new Point();
        weekPoint = new Point();

            if(width == 480){
                if (watchStyle == 0) {
                    if (widgetStyle == 0) {
                        hourPoint.set(128, 70);
                        hourSize = new Size(96, 94);

                        colonPoint.set(224, 70);
                        colonSize = new Size(32, 94);

                        minutePoint.set(256, 70);
                        minuteSize = new Size(96, 94);

                        datePoint.set(168, 182);
                        dateSize = new Size(56, 40);

                        weekPoint.set(241, 182);

                    } else {
                        hourPoint.set(96, 69);
                        hourSize = new Size(124, 96);

                        colonPoint.set(220, 69);
                        colonSize = new Size(40, 96);

                        minutePoint.set(260, 69);
                        minuteSize = new Size(124, 96);

                        datePoint.set(168, 183);
                        dateSize = new Size(56, 40);

                        weekPoint.set(241, 183);
                    }
                } else if (watchStyle == 1) {
                    if (widgetStyle == 0) {
                        hourPoint.set(237, 159);
                        hourSize = new Size(96, 94);

                        colonPoint.set(333, 159);
                        colonSize = new Size(32, 94);

                        minutePoint.set(365, 159);
                        minuteSize = new Size(96, 94);

                        datePoint.set(277, 271);
                        dateSize = new Size(56, 40);

                        weekPoint.set(350, 265);

                    } else {
                        hourPoint.set(166, 151);
                        hourSize = new Size(124, 96);

                        colonPoint.set(290, 151);
                        colonSize = new Size(40, 96);

                        minutePoint.set(330, 151);
                        minuteSize = new Size(124, 96);

                        datePoint.set(238, 265);
                        dateSize = new Size(56, 40);

                        weekPoint.set(311, 265);
                    }


                } else if (watchStyle == 2) {
                    if (widgetStyle == 0) {
                        hourPoint.set(17, 159);
                        hourSize = new Size(96, 94);

                        colonPoint.set(113, 159);
                        colonSize = new Size(32, 94);

                        minutePoint.set(145, 159);
                        minuteSize = new Size(96, 94);

                        datePoint.set(57, 271);
                        dateSize = new Size(56, 40);

                        weekPoint.set(130, 265);

                    } else {
                        hourPoint.set(8, 151);
                        hourSize = new Size(124, 96);

                        colonPoint.set(132, 151);
                        colonSize = new Size(40, 96);

                        minutePoint.set(172, 151);
                        minuteSize = new Size(124, 96);

                        datePoint.set(80, 265);
                        dateSize = new Size(56, 40);

                        weekPoint.set(153, 265);
                    }


                } else if (watchStyle == 3) {
                    if (widgetStyle == 0) {
                        hourPoint.set(128, 259);
                        hourSize = new Size(96, 94);

                        colonPoint.set(224, 259);
                        colonSize = new Size(32, 94);

                        minutePoint.set(256, 259);
                        minuteSize = new Size(96, 94);

                        datePoint.set(161, 371);
                        dateSize = new Size(56, 40);

                        weekPoint.set(241, 371);

                    } else {
                        hourPoint.set(96, 257);
                        hourSize = new Size(124, 96);

                        colonPoint.set(220, 257);
                        colonSize = new Size(40, 96);

                        minutePoint.set(260, 257);
                        minuteSize = new Size(124, 96);

                        datePoint.set(168, 371);
                        dateSize = new Size(56, 40);

                        weekPoint.set(241, 371);
                    }
                } else {
                    datePoint.set(168, 400);
                    dateSize = new Size(56, 40);

                    weekPoint.set(241, 400);
                }
            }else if(width == 466){
                if (watchStyle == 0) {
                    if (widgetStyle == 0) {
                        hourPoint.set(121, 70);
                        hourSize = new Size(96, 94);

                        colonPoint.set(217, 70);
                        colonSize = new Size(32, 94);

                        minutePoint.set(249, 70);
                        minuteSize = new Size(96, 94);

                        datePoint.set(161, 182);
                        dateSize = new Size(56, 40);

                        weekPoint.set(234, 182);

                    } else {
                        hourPoint.set(90, 69);
                        hourSize = new Size(124, 96);

                        colonPoint.set(214, 69);
                        colonSize = new Size(40, 96);

                        minutePoint.set(254, 69);
                        minuteSize = new Size(124, 96);

                        datePoint.set(161, 183);
                        dateSize = new Size(56, 40);

                        weekPoint.set(234, 183);
                    }
                } else if (watchStyle == 1) {
                    if (widgetStyle == 0) {
                        hourPoint.set(230, 152);
                        hourSize = new Size(96, 94);

                        colonPoint.set(326, 152);
                        colonSize = new Size(32, 94);

                        minutePoint.set(358, 152);
                        minuteSize = new Size(96, 94);

                        datePoint.set(270, 264);
                        dateSize = new Size(56, 40);

                        weekPoint.set(343, 264);

                    } else {
                        hourPoint.set(166, 151);
                        hourSize = new Size(124, 96);

                        colonPoint.set(290, 151);
                        colonSize = new Size(40, 96);

                        minutePoint.set(330, 151);
                        minuteSize = new Size(124, 96);

                        datePoint.set(238, 265);
                        dateSize = new Size(56, 40);

                        weekPoint.set(311, 265);
                    }


                } else if (watchStyle == 2) {
                    if (widgetStyle == 0) {
                        hourPoint.set(17, 152);
                        hourSize = new Size(96, 94);

                        colonPoint.set(113, 152);
                        colonSize = new Size(32, 94);

                        minutePoint.set(145, 152);
                        minuteSize = new Size(96, 94);

                        datePoint.set(57, 264);
                        dateSize = new Size(56, 40);

                        weekPoint.set(130, 264);

                    } else {
                        hourPoint.set(8, 151);
                        hourSize = new Size(124, 96);

                        colonPoint.set(132, 151);
                        colonSize = new Size(40, 96);

                        minutePoint.set(172, 151);
                        minuteSize = new Size(124, 96);

                        datePoint.set(80, 265);
                        dateSize = new Size(56, 40);

                        weekPoint.set(153, 265);
                    }


                } else if (watchStyle == 3) {
                    if (widgetStyle == 0) {
                        hourPoint.set(121, 269);
                        hourSize = new Size(96, 94);

                        colonPoint.set(217, 269);
                        colonSize = new Size(32, 94);

                        minutePoint.set(249, 269);
                        minuteSize = new Size(96, 94);

                        datePoint.set(161, 381);
                        dateSize = new Size(56, 40);

                        weekPoint.set(234, 381);

                    } else {
                        hourPoint.set(89, 257);
                        hourSize = new Size(124, 96);

                        colonPoint.set(213, 257);
                        colonSize = new Size(40, 96);

                        minutePoint.set(253, 257);
                        minuteSize = new Size(124, 96);

                        datePoint.set(161, 371);
                        dateSize = new Size(56, 40);

                        weekPoint.set(234, 371);
                    }
                } else {
                    datePoint.set(161, 400);
                    dateSize = new Size(56, 40);

                    weekPoint.set(234, 400);
                }
            }else{
                if (watchStyle == 0) {
                    if (widgetStyle == 0) {
                        hourPoint.set(93, 62);
                        hourSize = new Size(96, 94);

                        colonPoint.set(189, 62);
                        colonSize = new Size(32, 94);

                        minutePoint.set(221, 62);
                        minuteSize = new Size(96, 94);

                        datePoint.set(133, 174);
                        dateSize = new Size(56, 40);

                        weekPoint.set(206, 168);

                    } else {
                        hourPoint.set(61, 64);
                        hourSize = new Size(124, 96);

                        colonPoint.set(185, 64);
                        colonSize = new Size(40, 96);

                        minutePoint.set(225, 64);
                        minuteSize = new Size(124, 96);

                        datePoint.set(133, 178);
                        dateSize = new Size(56, 40);

                        weekPoint.set(206, 172);
                    }
                } else if (watchStyle == 1) {
                    if (widgetStyle == 0) {
                        hourPoint.set(93, 170);
                        hourSize = new Size(96, 94);

                        colonPoint.set(189, 170);
                        colonSize = new Size(32, 94);

                        minutePoint.set(221, 170);
                        minuteSize = new Size(96, 94);

                        datePoint.set(133, 282);
                        dateSize = new Size(56, 40);

                        weekPoint.set(206, 276);

                    } else {
                        hourPoint.set(61, 172);
                        hourSize = new Size(124, 96);

                        colonPoint.set(185, 172);
                        colonSize = new Size(40, 96);

                        minutePoint.set(225, 172);
                        minuteSize = new Size(124, 96);

                        datePoint.set(133, 286);
                        dateSize = new Size(56, 40);

                        weekPoint.set(206, 280);
                    }


                } else if (watchStyle == 2) {
                    if (widgetStyle == 0) {
                        hourPoint.set(42, 170);
                        hourSize = new Size(96, 94);

                        colonPoint.set(138, 170);
                        colonSize = new Size(32, 94);

                        minutePoint.set(170, 170);
                        minuteSize = new Size(96, 94);

                        datePoint.set(82, 282);
                        dateSize = new Size(56, 40);

                        weekPoint.set(155, 276);

                    } else {
                        hourPoint.set(10, 172);
                        hourSize = new Size(124, 96);

                        colonPoint.set(134, 172);
                        colonSize = new Size(40, 96);

                        minutePoint.set(174, 172);
                        minuteSize = new Size(124, 96);

                        datePoint.set(82, 286);
                        dateSize = new Size(56, 40);

                        weekPoint.set(155, 280);
                    }


                } else if (watchStyle == 3) {
                    if (widgetStyle == 0) {
                        hourPoint.set(93, 298);
                        hourSize = new Size(96, 94);

                        colonPoint.set(189, 298);
                        colonSize = new Size(32, 94);

                        minutePoint.set(221, 298);
                        minuteSize = new Size(96, 94);

                        datePoint.set(133, 410);
                        dateSize = new Size(56, 40);

                        weekPoint.set(206, 404);

                    } else {
                        hourPoint.set(61, 300);
                        hourSize = new Size(124, 96);

                        colonPoint.set(185, 300);
                        colonSize = new Size(40, 96);

                        minutePoint.set(225, 300);
                        minuteSize = new Size(124, 96);

                        datePoint.set(133, 414);
                        dateSize = new Size(56, 40);

                        weekPoint.set(206, 408);
                    }
                } else {
                    datePoint.set(133, 435);
                    dateSize = new Size(56, 40);

                    weekPoint.set(206, 429);
                }
            }

    }

    public String Resume() {
        //先初始化控件位置和大小
        fitPositionAndSize();
        StringBuilder sb = new StringBuilder();
        if (getWatchfaceStyle() == 4) {
            //日期
            String dateJS = groupResumeJs("_date", QjsWidget.DATE_NUM, getDateImagePrefix(), datePoint, dateSize, getStyle(), index);
            String weekJs = optionResumeJs("_week", QjsWidget.WEEK_DATA, getWeekImagePrefix(), weekPoint, index);
            sb.append(dateJS);
            sb.append(weekJs);
        } else {
            //创建时钟组合图
            String hourJS = groupResumeJs("_hour", QjsWidget.HOUR_NUM, getTimeImagePrefix(), hourPoint, hourSize, getStyle(), index);
            //冒号
//            String colonJS = imgJS("_colon", getColonPath(), colonPoint, index);
            //分钟
            String minuteJS = groupResumeJs("_minute", QjsWidget.MINUTE_NUM, getTimeImagePrefix(), minutePoint, minuteSize, getStyle(), index);
            //日期
            String dateJS = groupResumeJs("_date", QjsWidget.DATE_NUM, getDateImagePrefix(), datePoint, dateSize, getStyle(), index);
            //星期
            String weekJS = optionResumeJs("_week", QjsWidget.WEEK_DATA, getWeekImagePrefix(), weekPoint, index);

            sb.append(hourJS);
//            sb.append(colonJS);
            sb.append(minuteJS);
            sb.append(dateJS);
            sb.append(weekJS);
        }
        return sb.toString();
    }
}
